﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIBattleChat : UIObject
{
    public InputField inputFieldChat;
    public Text textChatPrefab;

 
    public void OnButtonChatClick(Button button)
    {
        var chatContent = inputFieldChat.text.Trim();
        if (string.IsNullOrEmpty(chatContent))
            return;
        var req = new actionbattle();
        req.type = ActionType.Chat;
        req.touid = "";//temp
        req.content = chatContent;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonChatPrefabClick()
    {
        var chatContent = textChatPrefab.text.Trim();
        if (string.IsNullOrEmpty(chatContent))
            return;
        var req = new actionbattle();
        req.type = ActionType.Chat;
        req.touid = "";//temp
        req.content = chatContent;
        WebSocketClient.Instance.SendSerialize(req);
    }
    private void OnHandleActionbattleRsp(System.Object data)
    {
        var rsp = data as actionbattle_rsp;
        if (rsp.ret != NetErrorCode.None)
        {
            UITips.Instance.Show("出错了:" + rsp.ret + " " + rsp.reason);
        }
    }

    private DataBattleMgr _dataBattleMgr;
    public void Bind(DataBattleMgr data)
    {
        _dataBattleMgr = data;
        foreach (var player in _dataBattleMgr.userid2data.Values)
        {
            player.OnStateChangeHandle += OnStateChangeHandle;

            if (player.GetUserId() != DataPlayer.Instance.userId)
                player.OnPlayerChallenge += OnPlayerChallenge;
        }  
    }

    public void Unbind()
    {
        foreach (var player in _dataBattleMgr.userid2data.Values)
        {
            player.OnStateChangeHandle -= OnStateChangeHandle;

            if (player.GetUserId() != DataPlayer.Instance.userId)
                player.OnPlayerChallenge -= OnPlayerChallenge;
        }
        _dataBattleMgr = null;
    }

    private void OnStateChangeHandle(BattlePlayerState state, string fromUserId, string toUserId)
    {
        //自己做的操作不处理
        if (fromUserId == DataPlayer.Instance.userId)
            return;

        switch (state)
        {
            case BattlePlayerState.Close:
                if (fromUserId != toUserId)
                    textChatPrefab.text = XLocalization.Get("迷途知返，愚子可教");
                break;
            case BattlePlayerState.Shake:
                if(fromUserId != toUserId)
                    textChatPrefab.text = XLocalization.Get("不能随便摇别人骰子！");
                break;
            case BattlePlayerState.Open:
                textChatPrefab.text = XLocalization.Get("开了骰子就不能反悔了");
                break;
            case BattlePlayerState.Glance:
                if (fromUserId != toUserId)
                    textChatPrefab.text = XLocalization.Get("偷看别人骰子，非君子所为");
                else
                    textChatPrefab.text = XLocalization.Get("你这样，我看到你的骰子了");
                break;
            case BattlePlayerState.LookByMySelf:
                textChatPrefab.text = XLocalization.Get("看自己骰子，记忆力不好了？");
                break;
            default:
                Debug.LogError("unhandle state:" + state);
                break;
        }

    }

    private void OnPlayerChallenge(string challenge)
    {
        if(Random.Range(0.0f, 1.0f) > 0.5)
            textChatPrefab.text = challenge + "?" + XLocalization.Get("你疯了吧？");
        else
            textChatPrefab.text = XLocalization.Get("还没轮到你喊，不要着急");
    }

    protected override void onVisible()
    {
        base.onVisible();
        textChatPrefab.text = XLocalization.Get("先摇塞子,我先喊!逆时针顺序喊");
        Facade.Instance.RegistNetEvent(nameof(actionbattle_rsp), OnHandleActionbattleRsp);
    }
    protected override void OnInvisible()
    {
        base.OnInvisible();
        Facade.Instance.UnRegistNetEvent(nameof(actionbattle_rsp), OnHandleActionbattleRsp);
    }
}